package cn.itcast.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ContextLoaderListener implements ServletContextListener {
    //监听应用域创建
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("检测到应用域创建，将要开始加载配置文件");
        //读取全局配置
        String configName = sce.getServletContext().getInitParameter("contextConfigLocation");
        if(configName.startsWith("classpath")){
            String filePath = ContextLoaderListener.class.getClassLoader().getResource(configName.substring(10)).getPath();
            System.out.println("正在解析"+filePath+"文件");
        }
    }

    //监听应用域销毁
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("检测到应用域销毁");
    }
}
